<%--
  Created by IntelliJ IDEA.
  User: ddd
  Date: 2018/4/22
  Time: 16:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户选题</title>
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/css/font-awesome.min93e3.css">
    <link rel="stylesheet" type="text/css" href="/static/css/admin.css">
</head>
<body>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="admin-container">
        <%--<table id="cartypetable" class="table table-hover table-striped" data-toggle="table" data-height="350"
               data-pagination="true" data-mobile-responsive="true"
               data-icon-size="outline" data-page-size="5" data-page-list="[5,10]">
            <thead>
            <th>车型</th>
            <th>科目</th>
            </thead>
            <tbody>
            <tr>
                <td>C1C2 C3C4</td>
                <td>
                    <table class="table">
                        <tr>
                            <td><i class="fa fa-file-text"></i> 科目一(9)</td>
                            <td>
                                <label><input type="checkbox" checked="">[1]标志基础&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[2]进阶练习&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[3]快速通道600&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[4]总复习&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[4]新增&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[5]精选考题&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[6]科一常识题&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[7]备用A&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[8]备用B&nbsp;&nbsp;</label>
                            </td>
                        </tr>
                        <tr>
                            <td><i class="fa fa-file-text"></i> 科目四(5)</td>
                            <td>
                                <label><input type="checkbox" checked="">[1]小车科四&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[1]小车科四速成600&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[1]DJ备用科目四&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[3]新增&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[4]新增1&nbsp;&nbsp;</label>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td>A1A3B1</td>
                <td>
                    <table class="table">
                        <tr>
                            <td><i class="fa fa-file-text"></i> 科目一(9)</td>
                            <td>
                                <label><input type="checkbox" checked="">[1]标志基础</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[2]进阶练习</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[3]客车科目一</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[4]快速通道600</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[5]备用A基础</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[6]备用B强化</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[7]精选考题</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[8]科一常识题</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[9]总复习</a>&nbsp;&nbsp;</label>
                            </td>
                        </tr>
                        <tr>
                            <td><i class="fa fa-file-text"></i> 科目四(2)</td>
                            <td>
                                <label><input type="checkbox" checked="">[1]客车科四1</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[2]客车科四2</a>&nbsp;&nbsp;</label>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td>A2B2</td>
                <td>
                    <table class="table">
                        <tr>
                            <td><i class="fa fa-file-text"></i> 科目一(8)</td>
                            <td>
                                <label><input type="checkbox" checked="">[1]标志基础 </a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[2]进阶练习 </a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[3]快速通道600</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[3]备用A基础</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[4]精选考题</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[5]科一常识</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[6]货车科目一</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[7]总复习</a>&nbsp;&nbsp;</label>
                            </td>
                        </tr>
                        <tr>
                            <td><i class="fa fa-file-text"></i> 科目四(2)</td>
                            <td>
                                <label><input type="checkbox" checked="">[1]货车科目四1</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[2]货车科目四 2</a>&nbsp;&nbsp;</label>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td>DEF</td>
                <td>
                    <table class="table">
                        <tr>
                            <td><i class="fa fa-file-text"></i> 科目一(6)</td>
                            <td>
                                <label><input type="checkbox" checked="">[1]摩托车科目一</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[2]地方题</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[3]JQ单元</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[4]机械车</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[5]货运资格证</a>&nbsp;&nbsp;</label>
                                <label><input type="checkbox" checked="">[10]演讲测试题2</a>&nbsp;&nbsp;</label>
                            </td>
                        </tr>
                        <tr>
                            <td><i class="fa fa-file-text"></i> 科目四(1)</td>
                            <td>
                                <label><input type="checkbox" checked="">[1]摩托车科目四</a>&nbsp;&nbsp;</label>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            </tbody>
        </table>--%>
        <input type="hidden" class="userids">
        <table id="cartypetable" data-mobile-responsive="true"></table>
        <button class="btn btn-success" onclick="AddPower()">确定</button>
    </div>

</div>
<script src="/static/js/jquery.min.js"></script>
<script src="/static/layer/layer.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<script src="/static/js/plugins/bootstrap-table/bootstrap-table.js"></script>
<script src="/static/js/plugins/bootstrap-table/bootstrap-table-mobile.js"></script>
<script src="/static/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script>

    var url = location.search.slice(5);

    function courseSelect(id) {
        $(".course" + id).each(function () {
            if (!$(".mcourse" + id).prop("checked")) {
                $(this).prop("checked", false);
            }
            else {
                $(this).prop('checked',true);

            }
        });
    }
    function carSelect(id) {
        $(".car" + id).each(function () {
            if (!$(".mcar" + id).prop("checked")) {
                $(this).prop("checked", false);
            }
            else {
                $(this).prop('checked',true);
            }
        });
    }


    $.ajax({
        type: 'GET',
        dataType: "json",
        data: {id: url},
        url: '/levelAndUserLe',
        success: function (result) {
            console.info(result);
            $('#cartypetable').bootstrapTable({
                data: result.allvos,
                method: 'GET',
                striped: true,
                cache: false,
                height: 690,
                pagination: true,
                sidePagination: "client",
                pageNumber: 1,
                pageSize: 15,
                pageList: [10, 15, 25],
                clickToSelect: true,
                uniqueId: "id",
                columns: [
                    {
                        title: '车型',
                        formatter: function (value, row) {
                            var uservos = result.uservos;
                            var dd = $("<div>");
                            var $span = $("<span>");
                            var $check = $("<input>").attr("type", "checkbox").addClass("mcar"+row.id).attr("onchange"," carSelect("+row.id+")");
                            dd.append($("<label>").addClass("car").append($check).append("<input type=\"hidden\" class='carid' value=" + row.id + ">").append($span));
                            //判断allvos里面是否有uservos的车型
                            for (var i = 0; i < uservos.length; i++) {
//                                console.info("fdsfsgfsdgfsd");
//                                console.info(uservos[i].id);

                                if (uservos[i].id == row.id) {
//                                    console.info(row.id);
                                    $check.attr('checked',true);
                                    break;
                                }
                            }
                            $span.html(row.title);
                            return dd.html();
                        }
                    },
                    {
                        field: '',
                        title: '科目',
                        formatter: function (value, row) {
                            var courseData = row.courseVos;
                            //对courseData对象进行重新排序
                            function JsonSort(courseData, key) {
                                for (var j = 1; j < courseData.length; j++) {
                                    var temp = courseData[j],
                                        val = temp[key],
                                        i = j - 1;
                                    while (i >= 0 && courseData[i][key] > val) {
                                        courseData[i + 1] = courseData[i];
                                        i = i - 1;
                                    }
                                    courseData[i + 1] = temp;
                                }
                                return courseData;
                            }

                            var courseData = JsonSort(courseData, 'id');

                            var $div = $("<div>");
                            var $course = $("<table>").addClass("table");
                            var uservos = result.uservos;
                            $div.append($course);
                            for (var j = 0; j < courseData.length; j++) {
                                var $check = $("<input>").attr("type", "checkbox").addClass("car"+row.id).addClass("mcourse"+courseData[j].id).attr("onchange","courseSelect("+courseData[j].id+")");
                                var $tr = $("<tr>");
                                $course.append($tr);
                                var $td1 = $("<td>").append($("<label>").addClass("course").append($check).append("<input type=\"hidden\" class='courseid' value=" + courseData[j].id + ">").append($("<i>").addClass("fa fa-file-text").html(courseData[j].name + "(" + courseData[j].exertypes.length + ")")));
                                $tr.append($td1);
                                //判断allvos里面是否有uservos的科目
                                for (var m = 0; m < uservos.length; m++) {
                                    for (var n = 0; n < uservos[m].courseVos.length; n++) {
                                        if (uservos[m].courseVos[n].id == courseData[j].id) {
                                            $check.attr('checked',true);
                                            break;
                                        }
                                    }
                                }

                                var $td2 = $("<td>");
                                $tr.append($td2);
                                for (var i = 0; i < courseData[j].exertypes.length; i++) {
                                    var $check = $("<input>").attr("type", "checkbox").addClass("car"+row.id).addClass("course"+courseData[j].id);
                                    if (courseData[j].exertypes[i].status === 1) {
                                        $td2.append($("<label>").addClass("exer").append($check).append("<input type=\"hidden\" class='exerid' value=" + courseData[j].exertypes[i].id + ">").append("[" + courseData[j].exertypes[i].px + "]" + courseData[j].exertypes[i].name));
                                        //判断allvos里面是否有uservos的题型
                                        for (var m = 0; m < uservos.length; m++) {
                                            for (var n = 0; n < uservos[m].courseVos.length; n++) {
                                                for (var z = 0; z < uservos[m].courseVos[n].exertypes.length; z++) {
                                                    if (uservos[m].courseVos[n].exertypes[z].id == courseData[j].exertypes[i].id) {
                                                        $check.attr('checked',true);
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            return $div.html();
                        }
                    }]
            });



        }
    });


    function AddPower() {
        var data = new Object();
        var ids =[];
        ids.push(url);
        var levels = [];
        data.ids = ids;
        var count = 0;
        var $car = $(".car");
        $.each($car, function (i, e) {
            var ischecked = $(this).find("input[type='checkbox']").is(':checked');
            if (ischecked) {
                levels[count] = new Object();
                var lid = $(this).find(".carid").val();
                levels[count].lid = lid;
                levels[count].type = 3;
                count++;
            }
//            console.info(levels);
        });


        var $course = $(".course");
        $.each($course, function (i, e) {
            var ischecked = $(this).find("input[type='checkbox']").is(':checked');
            if (ischecked) {
                levels[count] = new Object();
                var lid = $(this).find(".courseid").val();
                levels[count].lid = lid;
                levels[count].type = 2;
                count++;
            }
        });


        var $exer = $(".exer");
        $.each($exer, function (i, e) {

            var ischecked = $(this).find("input[type='checkbox']").is(':checked');
            if (ischecked) {
                levels[count] = new Object();
                var lid = $(this).find(".exerid").val();
                console.info(lid);
                levels[count].lid = lid;
                levels[count].type = 1;
                count++;
            }

        });

        data.levels = levels;
        console.info(data);

        $.ajax({
            contentType: 'application/json;charset=UTF-8',
            type: 'PUT',
            dataType: "json",
            data: JSON.stringify(data),
            url: '/level',
            success: function (result) {
                (function () {
                    layer.msg('操作成功', {icon: 1,time:1000});
                    setTimeout(function () {
                        var ind = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(ind);
                    },1000);
                })();
            }
        });
    }
</script>
</body>
</html>
